/**
 * File: index.ts
 * Project: reactssr
 * Created Date: Wednesday 2018/01/03 11:15:55
 * Author: zhengjiang
 * -----
 * Last Modified: Thursday 2018/01/04 13:46:48
 * Modified By: zhengjiang
 * -----
 * Description: 
 * 
 */
import { createStore, applyMiddleware, combineReducers } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import * as withRedux from 'next-redux-wrapper';
import nextReduxSaga from 'next-redux-saga';
import createSagaMiddleware from 'redux-saga';
import { all } from 'redux-saga/effects';
//#region 导入store
import { indexReducer, indexSaga } from './IndexStore/action';
//#endregion
const sagaMiddleware = createSagaMiddleware();

export const configureStore = (initialState = {}) => {
	const store = createStore(
		combineReducers({
			...indexReducer
		}),
		initialState,
		composeWithDevTools(applyMiddleware(sagaMiddleware))
	);

	(store as any).sagaTask = sagaMiddleware.run(function* saga() {
		yield all([ ...indexSaga ]);
	});
	return store;
};

export function withReduxSaga(BaseComponent) {
	return withRedux(configureStore)(nextReduxSaga(BaseComponent));
}
/**
 * action 方法定义
 */
export type IAction<T = {}> = (payload?: T) => any;
/**
 * action payload定义
 */
export type IActionPayload<T = {}> = { payload: T };

export interface IStoreProps {
	dispatch: Function;
}
